package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

import com.mvp.base.BaseAppCompatActivity;
import com.mvp.base.BaseDialog;
import com.mvp.dagger.modules.NetworkModule;
import com.mvp.utils.ToastUtil;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_ui.profile.activity.ResetPayPassActivity;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_widget.pass.PayPwdEditText;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2018/12/17.
 */
public class ForgotPayPassDialog extends BaseDialog {
    @BindView(R.id.paypwd_forgotpaypass_pass)
    PayPwdEditText payPwdEditText;
    @BindView(R.id.ll_forgotpay_inputcode)
    LinearLayout ll_forgotpay_inputcode;
    @BindView(R.id.ll_forgotpay_sendcode)
    LinearLayout ll_forgotpay_sendcode;
    private CompositeDisposable composite;
    private final ApiService apiService = NetworkModule.getRetrofit().create(ApiService.class);
    private final BaseAppCompatActivity activity;
    private String otpCodeForgotPass;//当用户是忘记了当前密码的情况下

    public ForgotPayPassDialog(Context context) {
        super(context);
        this.composite = new CompositeDisposable();
        activity = (BaseAppCompatActivity) context;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_forgotpaypass;
    }

    @Override
    protected void initView() {
        payPwdEditText.initStyle(R.drawable.edit_num_bg, 6, 0.33f, R.color.common_text_black, R.color.common_text_black, 20);
        ll_forgotpay_inputcode.setVisibility(View.GONE);
        ll_forgotpay_sendcode.setVisibility(View.VISIBLE);
        setCanceledOnTouchOutside(false);
    }

    @Override
    protected void initListener() {

    }

    private void sendCodeForResetPayPass() {
        Disposable subscribe = apiService.sendCodeForResetPayPass(UserInfo.getMemberId() + "")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    if (activity != null) {
                        activity.showLoadingBar(null);
                    }
                })
                .doAfterTerminate(() -> {
                    if (activity != null) {
                        activity.dismissLoadingBar("");
                    }
                })
                .subscribe(codeModelResultModel -> {
                    if (activity != null) {
                        activity.dismissLoadingBar("");
                    }
                    if (codeModelResultModel.isSuccess()) {
                        ll_forgotpay_inputcode.setVisibility(View.VISIBLE);
                        ll_forgotpay_sendcode.setVisibility(View.GONE);
                        otpCodeForgotPass = codeModelResultModel.getData().getOTP_code();
                    } else {
                        ToastUtil.showToast(codeModelResultModel.getMsg());
                        dismiss();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    if (activity != null) {
                        activity.dismissLoadingBar("");
                    }
                });
        composite.add(subscribe);
    }

    @OnClick({R.id.iv_dialog_close, R.id.bt_dialog_ok, R.id.tv_cancelsure_sure
            , R.id.tv_cancelsure_cancel})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancelsure_sure: {
                sendCodeForResetPayPass();
            }
            break;
            case R.id.tv_cancelsure_cancel:
            case R.id.iv_dialog_close: {
                dismiss();
            }
            break;
            case R.id.bt_dialog_ok:
                String code = payPwdEditText.getPwdText();
                Timber.d("--->>>>>>>>---code:"+code);
                Timber.d("--->>>>>>>>---otpCodeForgotPass:"+otpCodeForgotPass);
                if (!TextUtils.isEmpty(code) && code.length() == 6 && TextUtils.equals(otpCodeForgotPass, code)) {
                    if (activity != null) {
                        WToastHelper.showContentView(activity, R.string.changepaypass_success,R.drawable.icon_suo);
                    }
                    Intent intent = new Intent(getContext(), ResetPayPassActivity.class);
                    intent.putExtra("otp", otpCodeForgotPass);
                    getContext().startActivity(intent);
                    dismiss();
                } else {
                    ToastUtil.showToast(getString(R.string.invalid_code));
                    payPwdEditText.clearText();
                }
                break;
        }
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }
    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.8f);
    }
    @Override
    public void dismiss() {
        if (composite != null && !composite.isDisposed()) {
            composite.dispose();
        }
        super.dismiss();
    }
}
